package com.nut.web;

import com.nut.common.Result;
import com.nut.dao.OrderDao;
import com.nut.dao.OrderItemDao;
import com.nut.dao.impl.OrderDaoImpl;
import com.nut.dao.impl.OrderItemDaoImpl;
import com.nut.pojo.Order;
import com.nut.pojo.OrderItem;
import com.nut.service.OrderService;
import com.nut.service.UserService;
import com.nut.service.impl.OrderServiceImpl;
import com.nut.service.impl.UserServiceImpl;
import com.nut.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

public class clientOrderServlet extends BaseServlet {
    OrderService orderService = new OrderServiceImpl();
    UserService userService = new UserServiceImpl();

    public static String getPostData(HttpServletRequest request) {
        StringBuffer data = new StringBuffer();
        String line;
        BufferedReader reader;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine()))
                data.append(line);
        } catch (IOException e) {
        } finally {
        }
        return data.toString();
    }

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String string = getPostData(req);
        String orderId = orderService.createOrder(string);
        resp.getWriter().write(JsonUtils.toJson(Result.ok().data("orderId", orderId)));
    }

    /**
     * 查询我的订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        if (username == null) {
            resp.getWriter().write(JsonUtils.toJson(Result.error().message("login为null")));
        } else {
            Integer id = userService.getUserIdByUsername(username);
            List<Order> order = orderService.showMyOrders(id);
            resp.getWriter().write(JsonUtils.toJson(Result.ok().data("orders", order)));
        }
    }

    /**
     * 查看订单详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        resp.getWriter().write(JsonUtils.toJson(Result.ok().data("orderItems", orderItems)));
    }

    /**
     * 收货
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        orderService.receiveOrder(orderId);
        resp.sendRedirect(req.getHeader("referer"));
    }
}
